package com.ysd.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.Service.ModulesServcie;
import com.ysd.entity.Modules;
import com.ysd.util.Treemodule;

@RestController
@RequestMapping("/modulesss")
@CrossOrigin 
public class modulesController {
	@Autowired
	private ModulesServcie modulesService;
	

	@GetMapping("/tree")
	public Map<Object, Object> findModulesTree() {
		Map<Object, Object> restMap = new HashMap<Object, Object>();

		List<Treemodule> tList = modulesService.findModulesTree();
		restMap.put("data", tList);
		restMap.put("message", "成功");
		restMap.put("code", "0");
		return restMap;
	}
	//修改模块
	@PutMapping("/upmodules")
	public Map<Object, Object> InsertOrUpdateModules(Modules modules) {
		Map<Object, Object> restMap = new HashMap<Object, Object>();
			// 修改
			// 获取该模块原数据
			Modules m = modulesService.findModulesById(modules.getMid());
			// 修改原数据内容
			m.setMname(modules.getMname());
			m.setPath(modules.getPath());
			// 修改模块
			modules = modulesService.insertOrUpdateModules(m);
			restMap.put("data", modules);
			restMap.put("code", "0");
			restMap.put("message", "模块修改成功");

		
		return restMap;
	}
	//添加模块
	@PostMapping("/addmodules")
	public Map<Object, Object> InsertOrUpdateModules(Modules modules, Integer pid) {
		System.out.println(pid);
		Map<Object, Object> restMap = new HashMap<Object, Object>();
			// 新增
			// 判断新增一级或其他
			if (pid != null) { 
				// 新增其他级模块
				// 通过父ID获取父模块信息
				Modules parent = modulesService.findModulesById(pid);
				modules.setParent(parent);
				// 新增模块信息
				modules = modulesService.insertOrUpdateModules(modules);
				restMap.put("data", modules);
				restMap.put("code", "0");
				restMap.put("message", "子模块新增成功");

			} else { 

			} 


		return restMap;
	}
	
	//删除模块
	

	@DeleteMapping("/delmodules")
	public Map<Object, Object> delModulesById(Integer id) {
		Map<Object, Object> restMap = new HashMap<Object, Object>();
		boolean decide = true;

		// 获取所有模块信息
		List<Modules> mList = modulesService.findAllModules();
		for (Modules m : mList) {
			if (m.getParent() != null) {
				if (((Modules) m.getParent()).getMid().equals(id)) {
					decide = false;
					restMap.put("code", "1");
					restMap.put("message", "删除失败，该模块下有子模块存在");
				}
			}

		}

		if (decide) {
			Integer r = modulesService.delModulesById(id);
			if (r > 0) {
				restMap.put("code", "0");
				restMap.put("message", "删除模块成功");
			} else {
				restMap.put("code", "1");
				restMap.put("message", "删除模块失败，未知错误");
			}
		}

		return restMap;
	}
	
}
